package com.library.service.impl;


import com.library.common.core.domain.AjaxResult;
import com.library.common.core.domain.BaseEntity;
import com.library.common.core.text.Convert;
import com.library.domain.MbGift;
import com.library.domain.MbGiftConvert;
import com.library.mapper.MbGiftConvertMapper;
import com.library.mapper.MbGiftMapper;
import com.library.service.IMbGiftConvertService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 礼品兑换 服务层实现
 *
 * @author null
 * @date 2020-04-23
 * @blame Android Team
 */
@Slf4j
@Service
@SuppressWarnings("all")
@Transactional(rollbackFor = Exception.class)
public class MbGiftConvertServiceImpl implements IMbGiftConvertService {
    @Autowired
    private MbGiftConvertMapper mbGiftConvertMapper;

    @Autowired
    private MbGiftMapper giftMapper;

    /**
     * 兑换数量
     */
    private static final int CONVERTNUM = 1;

    /**
     * 查询礼品兑换信息
     *
     * @param id 礼品兑换ID
     * @return 礼品兑换信息
     */
    @Override
    public MbGiftConvert selectMbGiftConvertById(Integer id) {
        return mbGiftConvertMapper.selectMbGiftConvertById(id);
    }

    /**
     * 查询礼品兑换列表
     *
     * @param mbGiftConvert 礼品兑换信息
     * @return 礼品兑换集合
     */
    @Override
    public List<MbGiftConvert> selectMbGiftConvertList(MbGiftConvert mbGiftConvert) {
        return mbGiftConvertMapper.selectMbGiftConvertList(mbGiftConvert);
    }

    /**
     * 新增礼品兑换
     *
     * @param mbGiftConvert 礼品兑换信息
     * @return 结果
     */
    @Override
    public int insertMbGiftConvert(MbGiftConvert mbGiftConvert) {
        return mbGiftConvertMapper.insertMbGiftConvert(mbGiftConvert);
    }

    /**
     * 修改礼品兑换
     *
     * @param mbGiftConvert 礼品兑换信息
     * @return 结果
     */
    @Override
    public int updateMbGiftConvert(MbGiftConvert mbGiftConvert) {
        return mbGiftConvertMapper.updateMbGiftConvert(mbGiftConvert);
    }

    /**
     * 删除礼品兑换对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMbGiftConvertByIds(Long[] ids) {
        for (Long id : ids) {
            MbGiftConvert mbGiftConvert = mbGiftConvertMapper.selectMbGiftConvertById(id.intValue());
            if (null == mbGiftConvert)
                continue;
            mbGiftConvert.setIsDelete(BaseEntity.DELETE);
            mbGiftConvertMapper.updateMbGiftConvert(mbGiftConvert);
        }
        return BaseEntity.DELETE;
    }

    /**
     * 礼品兑换
     *
     * @return
     */
    @Override
    public AjaxResult mbGiftConvertByMembers(Long[] ids) {
        for (Long id : ids) {
            MbGiftConvert giftConvert = mbGiftConvertMapper.selectMbGiftConvertById(id.intValue());
            if (null == giftConvert) {
                log.error("mbGiftConvertByMembers is error ! id is " + id);
                return AjaxResult.error("兑换信息错误");
            }
            giftConvert.setConvertStatus(MbGiftConvert.CHANGE);
            int i = mbGiftConvertMapper.updateMbGiftConvert(giftConvert);
            MbGift mbGift = giftMapper.selectMbGiftById(giftConvert.getGiftId());
            Integer convertNum = mbGift.getConvertNum();
            Integer noConvertNum = mbGift.getNoConvertNum();
            if (noConvertNum <= 0) {
                log.error("noConvertNum Less than zero ! id is " + id);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("兑换信息错误，礼品数量不足");
            }
            mbGift.setConvertNum(convertNum + CONVERTNUM);
            mbGift.setNoConvertNum(noConvertNum - CONVERTNUM);
            int j = giftMapper.updateMbGift(mbGift);
            if (i <= 0 || j <= 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error();
            }
        }
        return AjaxResult.success();
    }

}
